package com.aloda.controller.user;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.constant.ErrorCode;
import com.aloda.constant.UserConstant;
import com.aloda.dto.app.AppAddDTO;
import com.aloda.dto.app.AppEditDTO;
import com.aloda.query.AppScrollingPageDTO;
import com.aloda.query.PageQuery;
import com.aloda.query.SearchPageDTO;
import com.aloda.result.PageResult;
import com.aloda.result.Result;
import com.aloda.service.AppBrowseService;
import com.aloda.service.AppService;
import com.aloda.service.CollectService;
import com.aloda.vo.app.AppBrowseVO;
import com.aloda.vo.app.AppDetailVO;
import com.aloda.vo.app.AppCardVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@Slf4j
@RestController("app-user")
@RequestMapping("/user/app")
public class AppController {

    @Resource
    private AppService appService;

    @Resource
    private CollectService collectService;

    @Resource
    private AppBrowseService appBrowseService;

    /**
     * 获取首页推荐应用列表
     *
     * @return
     */
    @GetMapping("/recommend")
    public Result<List<AppCardVO>> getRecommendAppList() {
        List<AppCardVO> recommendAppList = appService.getRecommendAppList();
        return Result.success(recommendAppList);
    }

    /**
     * 通过id获取应用详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public Result<AppDetailVO> getAppDetailById(@PathVariable Long id) {
        if (id == null) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(appService.getAppDetailById(id));
    }

    /**
     * 根据app分类滚动分页查询
     *
     * @param appScrollingPageDTO
     * @return
     */
    @PostMapping("/scroll")
    public Result<List<AppCardVO>> getAppScrollingPageList(@RequestBody AppScrollingPageDTO appScrollingPageDTO) {
        if (BeanUtil.isEmpty(appScrollingPageDTO)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        if (appScrollingPageDTO.getCurrentPage() > UserConstant.MAX_CURRENT_PAGE) {
            return Result.success(Collections.emptyList());
        }
        return Result.success(appService.getAppScrollingPageList(appScrollingPageDTO));
    }

    /**
     * 用户端新增应用
     *
     * @param appAddDTO
     * @return
     */
    @PostMapping
    public Result<Long> addApp(@RequestBody AppAddDTO appAddDTO) {
        log.info("用户端新增应用:{}", appAddDTO);
        if (BeanUtil.isEmpty(appAddDTO)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(appService.addAppByUser(appAddDTO));
    }

    /**
     * 用户端编辑应用
     *
     * @param appEditDTO
     * @return
     */
    @PutMapping
    public Result<Long> editApp(@RequestBody AppEditDTO appEditDTO) {
        log.info("用户端编辑应用:{}", appEditDTO);
        if (BeanUtil.isEmpty(appEditDTO)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(appService.editAppByUser(appEditDTO));
    }

    /**
     * 用户端删除应用
     *
     * @param id 删除应用的id
     * @return 删除数量
     */
    @DeleteMapping("/{id}")
    public Result deleteApp(@PathVariable Long id) {
        log.info("用户端删除应用:{}", id);
        if (id == null) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(appService.deleteAppByUser(id));
    }

    /**
     * 用户端设置应用分析查看权限
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/auth/{id}/{status}")
    public Result setAppAnalysisAuth(@PathVariable Long id, @PathVariable Integer status) {
        log.info("用户端设置应用分析查看权限，应用id：{}，权限：{}", id, status);
        if (id == null || status == null) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(appService.setAnalysisAuth(id, status));
    }

    /**
     * 根据关键词搜索应用
     *
     * @param searchPageDTO
     * @return
     */
    @PostMapping("/search")
    public Result<List<AppCardVO>> searchApp(@RequestBody SearchPageDTO searchPageDTO) {
        if (BeanUtil.isEmpty(searchPageDTO)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        if (searchPageDTO.getCurrentPage() > UserConstant.MAX_CURRENT_PAGE) {
            return Result.success(Collections.emptyList());
        }
        return Result.success(appService.searchApp(searchPageDTO));
    }

    /**
     * 增加app浏览量
     *
     * @param id
     * @return
     */
    @PostMapping("/browse/{id}")
    public Result<Boolean> addAppBrowse(@PathVariable Long id) {
        if (id == null) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(appBrowseService.addAppBrowse(id));
    }

    /**
     * 获取app浏览排行榜
     *
     * @return
     */
    @GetMapping("/browse")
    public Result<List<AppBrowseVO>> getAppBrowseTop() {
        return Result.success(appBrowseService.getAppBrowseTop());
    }

    /**
     * 获取用户页推荐的应用列表
     *
     * @return
     */
    @GetMapping("/recommend/user")
    public Result<List<AppBrowseVO>> getAppRecommendListInUserPage() {
        return Result.success(appService.getAppRecommendListInUserPage());
    }

    /**
     * 通过用户id获取App列表分页查询
     *
     * @param id
     * @return
     */
    @PostMapping("/user/{id}")
    public Result<PageResult<AppCardVO>> getAppListByUserId(@PathVariable Long id, @RequestBody PageQuery pageQuery) {
        if (id == null || BeanUtil.isEmpty(pageQuery)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        if (pageQuery.getCurrentPage() > UserConstant.MAX_CURRENT_PAGE) {
            return Result.error("最多查询100页数据");
        }
        return Result.success(appService.getAppListByUserId(id, pageQuery));
    }

    /**
     * 分页获取当前登录者创建的应用
     *
     * @return
     */
    @PostMapping("/user/me")
    public Result<PageResult<AppCardVO>> getAppListByCreateUser(@RequestBody PageQuery pageQuery) {
        if (BeanUtil.isEmpty(pageQuery)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        // 本人查看不用校验最大页数
        return Result.success(appService.getAppListByToken(pageQuery));
    }

    /**
     * 收藏应用
     *
     * @param id
     * @return
     */
    @PostMapping("/collect/{id}")
    public Result<Boolean> collectApp(@PathVariable Long id) {
        if (id == null || id < 0) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(collectService.collectApp(id));
    }

    /**
     * 取消收藏应用
     *
     * @param id
     * @return
     */
    @PostMapping("/uncollect/{id}")
    public Result<Boolean> unCollectApp(@PathVariable Long id) {
        if (id == null || id < 0) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(collectService.unCollectApp(id));
    }

    /**
     * 分页查找当前用户收藏的应用
     *
     * @param pageQuery
     * @return
     */
    @PostMapping("/collect/page")
    public Result<PageResult<AppCardVO>> pageQueryCollectAppOfCreateUser(@RequestBody PageQuery pageQuery) {
        if (BeanUtil.isEmpty(pageQuery)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(appService.pageQueryCollectAppOfCreateUser(pageQuery));
    }

    /**
     * 根据用户id分页查询他创建的应用
     * @param id
     * @param pageQuery
     * @return
     */
    @PostMapping("/collect/user/{id}")
    public Result<PageResult<AppCardVO>> pageQueryCollectAppById(@PathVariable Long id, @RequestBody PageQuery pageQuery) {
        if (id == null || BeanUtil.isEmpty(pageQuery)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(appService.pageQueryCollectAppById(id, pageQuery));
    }
}
